package cn.jolyoulu.adminweb.controller.sys;


import cn.jolyoulu.adminweb.entity.bo.form.SysMenuFormBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.jolyoulu.common.web.security.permission.Permission;
import cn.jolyoulu.adminweb.entity.vo.SysMenuVO;
import cn.jolyoulu.adminweb.service.ApiSysMenuService;
import cn.jolyoulu.common.web.entity.ResultInfo;

import java.util.List;

/**
 * 系统管理/菜单管理
 */
@RestController
@RequestMapping("/api/sys/menu")
public class ApiSysMenuController {

    @Autowired
    private ApiSysMenuService apiSysMenuService;

    /**
     * 菜单树结构
     */
    @Permission({"sys:menu:basic"})
    @GetMapping("/tree")
    public ResultInfo<List<SysMenuVO>> tree(){
        List<SysMenuVO> res = apiSysMenuService.tree();
        return ResultInfo.success(res);
    }

    /**
     * 菜单信息
     */
    @Permission({"sys:menu:basic"})
    @GetMapping("/get/{id}")
    public ResultInfo<SysMenuVO> getById(@PathVariable String id) {
        SysMenuVO res = apiSysMenuService.getById(id);
        return ResultInfo.success(res);
    }

    /**
     * 添加菜单
     */
    @Permission({"sys:menu:add"})
    @PostMapping("/add")
    public ResultInfo add(@RequestBody @Validated SysMenuFormBO sysMenuFormBO){
        apiSysMenuService.add(sysMenuFormBO);
        return ResultInfo.success();
    }

    /**
     * 编辑菜单
     */
    @Permission({"sys:menu:edit"})
    @PutMapping("/edit/{id}")
    public ResultInfo edit(@PathVariable String id,@RequestBody SysMenuFormBO sysMenuFormBO){
        sysMenuFormBO.setId(id);
        apiSysMenuService.update(sysMenuFormBO);
        return ResultInfo.success();
    }

    /**
     * 删除菜单
     */
    @Permission({"sys:menu:del"})
    @DeleteMapping("/del")
    public ResultInfo del(@RequestBody List<String> ids){
        apiSysMenuService.delByIds(ids);
        return ResultInfo.success();
    }
}
